
import gradio as gr
from flair.data import Sentence
from flair.models import SequenceTagger
import torch
import flair

# 加载命名实体识别模型和情感分析模型
tagger = SequenceTagger.load("./models/en-ner-conll03-v0.4.pt")


def analyze_sentence(input_text):
    # 创建Sentence对象
    tagger_sentence = Sentence(input_text)

    # 使用命名实体识别模型进行预测
    tagger.predict(tagger_sentence)

    # 提取NER结果
    ner_result = "\n".join([str(label) for label in tagger_sentence.labels])

    return ner_result


# 创建Gradio界面
iface = gr.Interface(
    fn=analyze_sentence,
    inputs=gr.Textbox(label="Enter a sentence"),
    outputs=[
        gr.Textbox(label="Named Entities"),
    ],
    examples=[
        ["Our family took a trip to Washington DC and Hawaii this summer and it was a memorable one.",]
    ],
    title="Sentence Analysis with Flair",
    description="Input a sentence and see the named entities and sentiment analysis results."
)

# # 启动Gradio应用
# iface.launch()
# 改为
iface.launch()